package com.it.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.it.ProductService;
import com.it.dao.ProductGuestMapper;
import com.it.dao.ProductMapper;
import com.it.pojo.Product;
import com.it.pojo.ProductGuest;
import com.it.pojo.params.product.ProductQuery;
import com.it.pojo.params.product.ProductSearchQuery;
import com.it.pojo.vo.article.ArticleProductVO;
import com.it.pojo.vo.product.ProductDetailVO;
import com.it.pojo.vo.product.ProductTicketVO;
import com.it.pojo.vo.product.ProductTypeVO;
import com.it.pojo.vo.product.ProductVO;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class ProductServiceImpl implements ProductService {

    @Resource
    private ProductMapper productMapper;


    @Resource
    private ProductGuestMapper productGuestMapper;

    @Override
    public List<ProductVO> listProductVO(ProductQuery productQuery) {
        Integer start = (productQuery.getPageNum() - 1) * productQuery.getLimit();
        Integer limit = productQuery.getLimit();
        Integer cityId = productQuery.getCityId();
        Integer provinceId = productQuery.getProvinceId();
        String time = productQuery.getTime();
        String nextTime = "";
        if (!"".equals(time)) {
            if (!(time.equals("10") || time.equals("11") || time.equals("12"))) {
                time = "0" + time;
            }
            Calendar cal = Calendar.getInstance();
            int year = cal.get(Calendar.YEAR);
            nextTime = year + "-" + time + "-" + "32";
            time = year + "-" + time;
        }
        System.out.println("time = " + time);
        System.out.println("nextTime = " + nextTime);
        List<ProductVO> productVOS = productMapper.listProductVO(start, limit, provinceId, cityId, time, nextTime);

        return productVOS;
    }

    @Override
    public Map<String, Object> listproductsById(Integer productId) {
        Map<String, Object> map = new HashMap<>();
        ProductDetailVO productDetailVO = productMapper.listproductsById(productId);
        LambdaQueryWrapper<ProductGuest> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ProductGuest::getProductId, productId);
        List<ProductGuest> productGuests = productGuestMapper.selectList(wrapper);
        List<ArticleProductVO> articleProductVOList = productMapper.listArticleByProductId(productId);
        Integer total = productMapper.getProductDetailUserTotal(productId);
        map.put("product", productDetailVO);
        map.put("productGuestList", productGuests);
        map.put("articleList", articleProductVOList);
        map.put("total", total);
        return map;


    }

    @Override
    public String getProductNameById(Integer productId) {
        LambdaQueryWrapper<Product> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Product::getProductId, productId);
        Product product = productMapper.selectOne(wrapper);
        if (product != null) {
            return product.getProductName();
        }
        return null;
    }

    @Override
    public ProductTicketVO getProductTicketInfo(Integer productId) {
        ProductTicketVO productTicketVO = productMapper.getProductTicketInfo(productId);
        return productTicketVO;
    }

    @Override
    public Map<String, Object> listProductTypeVO(Integer typeId, Integer condtion, String keyWords) {
        String finkey = new String();
        if (StringUtils.isNotBlank(keyWords)) {
            finkey = keyWords;
            keyWords = "%" + keyWords + "%";
        }
        Map<String, Object> map = new HashMap<>();
        List<ProductTypeVO> productTypeVOList = productMapper.listProductTypeVO(typeId, condtion, keyWords);
        String finalFinkey = finkey;
        List<ProductTypeVO> collect = productTypeVOList.stream().map(productTypeVO -> {
            String productName = productTypeVO.getProductName();
            if (productName != null) {
                if (StringUtils.isNotBlank(finalFinkey)) {
                    String replace = productName.replace(finalFinkey, "<span style='color:#F73131'>" + finalFinkey + " </span>");
                    productTypeVO.setProductName(replace);

                }
            }
            return productTypeVO;
        }).collect(Collectors.toList());
        map.put("productList", collect);
        return map;
    }

    @Override
    public ProductTypeVO listProductTypeVOById(Integer productId) {
        ProductTypeVO productTypeVO = productMapper.listProductTypeVOById(productId);
        return productTypeVO;
    }

    @Override
    public Map<String, Object> listProductVOByKeyWords(ProductSearchQuery productSearchQuery) {
        Integer start = (productSearchQuery.getPageNum() - 1) * productSearchQuery.getLimit();
        Integer limit = productSearchQuery.getLimit();
        String keyWords = productSearchQuery.getKeyWords();
        String finKey = new String();
        if (StringUtils.isNotBlank(keyWords)) {
            finKey = keyWords;
            keyWords = "%" + keyWords + "%";
        }
        Map<String, Object> map = new HashMap<>();
        List<ProductVO> productVOS = productMapper.listProductByKeyWords(start, limit, keyWords);
        Integer total = productMapper.listProductByKeyWordsTotal(keyWords);
        String finalFinKey = finKey;
        List<ProductVO> collect = productVOS.stream().map(productVO -> {
            String productName = productVO.getProductName();
            if (productName != null) {
                if (StringUtils.isNotBlank(finalFinKey)) {
                    String replace = productName.replace(finalFinKey, "<span style='color:#F73131'>" + finalFinKey + " </span>");
                    productVO.setProductName(replace);

                }
            }
            return productVO;
        }).collect(Collectors.toList());
        map.put("productList", collect);
        map.put("total", total);
        return map;
    }

    public static void main(String[] args) {
        String s = "胡挑夹子-上海站01.26";
        String key = "上海站";
        String replace = s.replace(key, "<span style='color:red'> " + key + " </span>");
        System.out.println("replace = " + replace);
        System.out.println("s = " + s);

    }
}
